//
//  BLTabBar.m
//  百思不得姐
//
//  Created by hbl on 16/3/14.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLTabBar.h"
#import "BLPublishViewController.h"
@interface BLTabBar()
@property (nonatomic, weak)UIButton *publishButton;
@end

@implementation BLTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIButton *publishButton = [[UIButton alloc]init];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [self addSubview:publishButton];
        self.publishButton = publishButton;
        [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return  self;
}

- (void)publishClick
{
    BLPublishViewController *publish = [[BLPublishViewController alloc]init];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:publish animated:YES completion:nil];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.publishButton.bounds = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
    self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    
    int index = 0;
    CGFloat btnH = self.bounds.size.height;
    CGFloat btnW = self.bounds.size.width / 5;
    CGFloat btnY = 0;
    for (UIView *btn in self.subviews) {
        if ([btn isKindOfClass: NSClassFromString(@"UITabBarButton")]){
             CGFloat btnX = btnW * index;
            if (index>1) {
                btnX = btnW * (index + 1);
            }
           
            btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
            index++;
        }
        
    }
}


@end
